// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

// Declares constants for null flag polarity.

#include <pollux/common/base/bit_util.h>

namespace kumo::pollux::bits {
    // Indicates a null value in a nulls bitmap.
    constexpr bool kNull = false;
    constexpr bool kNotNull = !kNull;

    // Use for initialization with memset.
    constexpr char kNullByte = 0;
    constexpr char kNotNullByte = 0xff;

    // Null flags are generally uint64_t for efficient bit counting.
    constexpr uint64_t kNull64 = 0UL;
    constexpr uint64_t kNotNull64 = (~0UL);

    inline bool isBitNull(const uint64_t *bits, int32_t index) {
        return isBitSet(bits, index) == kNull;
    }

    inline void set_null(uint64_t *bits, int32_t index) {
        clearBit(bits, index);
    }

    inline void clearNull(uint64_t *bits, int32_t index) {
        setBit(bits, index);
    }

    inline void set_null(uint64_t *bits, int32_t index, bool isNull) {
        setBit(bits, index, !isNull);
    }

    inline uint64_t
    countNonNulls(const uint64_t *nulls, int32_t begin, int32_t end) {
        return countBits(nulls, begin, end);
    }

    inline uint64_t count_nulls(const uint64_t *nulls, int32_t begin, int32_t end) {
        return (end - begin) - countNonNulls(nulls, begin, end);
    }
} // namespace kumo::pollux::bits
